package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrderService;
import com.sun.org.apache.xpath.internal.operations.Or;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderDetailService orderDetailService;

    @PostMapping("/submit")
    public R submit(@RequestBody Orders orders){
        orderService.submit(orders);
        return R.success();
    }

    @GetMapping("userPage")
    public R<Page<OrdersDto>> userPage(@RequestParam(required = false , defaultValue = "1") Integer page ,
                                 @RequestParam(required = false , defaultValue = "1") Integer pageSize){
        Page<OrdersDto> ordersDtoPage = orderService.userPage(page, pageSize);
        return R.success(ordersDtoPage);
    }

    @GetMapping("page")
    public R<Page<Orders>> getOrder (@RequestParam(required = false , defaultValue = "1") Integer page ,
                                     @RequestParam(required = false , defaultValue = "10") Integer pageSize,
                                     String Number,
                                     @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime,
                                     @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime){
        //创建page
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        //查询
        LambdaQueryWrapper<Orders> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Number != null , Orders::getNumber , Number);
        lambdaQueryWrapper.eq(beginTime != null , Orders::getOrderTime,beginTime);
        lambdaQueryWrapper.eq(endTime != null , Orders::getCheckoutTime ,endTime);
        lambdaQueryWrapper.orderByDesc(Orders::getOrderTime);
        Page<Orders> page1 = orderService.page(ordersPage, lambdaQueryWrapper);

        return R.success(page1);
    }

    @PutMapping
    public R putOrder(@RequestBody Orders orders){

        Orders orderServiceById = orderService.getById(orders);

        orderServiceById.setStatus(orders.getStatus());

        orderService.updateById(orderServiceById);

        return R.success();

    }
}
